<script type="text/javascript">
// 点击行，选中行
$(document).on('click', '.checkcity', function() {
    var checkTwo = $(this).find("td input[type='checkbox']");  
    if(checkTwo){  
        var flag = checkTwo[0].checked;  
        if(flag){  
            checkTwo.iCheck('uncheck');
        }else{  
            checkTwo.iCheck('check');
        };
    };
});

// 翻页
$("#tree_list").delegate(".pagination a","click",function(){
    $.get(this.href,function(html){
        $("#tree_list").replaceWith(html);
    });
    return false;
});

/*搜索
* url URL地址
*/
function tree_search(url){
    url = url + '&keyword=' + $('#keyword').val();
    $.get(url,function(html){
        $("#tree_list").replaceWith(html);
    });
}

/*点击展开子分类
* id ID
* title 标题
* tabledir 数据表
*/
function display_child(id,title,tabledir){
    var html = ''; var hide_id = ''; var tree = '';
    var parentid = '{:input('parentid')}';
    var sortid = '{:input('sortid')}';
    $.ajax({
        type: "POST",
        url: "{:url('common/child')}",
        dataType: "json",
        data: {tabledir:tabledir, id:id},
        success: function(data){
            $.each(data,function(i,val){
                tree = '';
                for(var j=0;j<val.level;j++){
                    tree += '|---';
                }
                html += '<tr id="tr_'+val.id+'" class="checkcity cursor">';
                html += '    <td>';
                html += '        <label>';
                html += '           <input type="checkbox" name="id[]" value="'+val.id+'">';
                html += '           <span class="d-invisible">'+val.id+'</span>';
                html += '        </label>';
                html += '    </td>';
                if(val.child == 'yes'){
                    html += '    <td id="td_'+val.id+'"><a onclick="javascript:display_child('+val.id+',\''+val.title+'\',\''+tabledir+'\');" href="javascript:void(0);">'+tree+val.title+'<i class="fa fa-plus-square fa-lg ml-2"></i></a></td>';
                }else{
                    html += '    <td>'+tree+val.title+'</td>';
                }
                html += '</tr>';
                hide_id = val.hide_id;
            });
            $('#tr_'+id+'').after(html);
            var parent_tree = tree.substring(0,tree.length-4);
            $('#td_'+id+'').html('<a onclick="javascript:hide_child('+id+',\''+hide_id+'\',\''+title+'\',\''+parent_tree+'\',\''+tabledir+'\');" href="javascript:void(0);">'+parent_tree+title+'<i class="fa fa-plus-square fa-lg ml-2"></i></a>');
        }
    });
}

// 确认选中
$('#check_close').click(function(){
    var html = '';
    $("input[name='id[]']:checked").each(function(){
        html += $(this).val() + ',';
    });
    html = html.substring(0,html.length-1);
    if(html == 0 || html == 'undefined'){
        html = 0;
    }
    $('#cityid').val(html);
    $("#modal").modal("hide");
});
</script>
{include file="public/js_hide_child" /}